//
//  main.swift
//  PrototypePattern
//
//  Created by mac on 2017/11/1.
//  Copyright © 2017年 RRC. All rights reserved.
//

import Foundation

print("Hello, World!")

//获得指定长度的随机字符串
func getRandString(maxLength:Int) -> String {
    
    let source = "abcdefghijklmnopqrskuvwxyzABCDEFGHIJKLMNOPQF"
    var sb = String()
    for _ in 0..<maxLength {
        let index = arc4random()%UInt32(source.count)
        sb.append(source[source.index(source.startIndex, offsetBy:
            String.IndexDistance(index))])
    }
    return sb
    
}

//发送邮件
func sendMail(mail:Mail) {
    print("标题"+mail.subject+"\t收件人："+mail.receiver+"\t...发送成功")
}

private let MAX_COUNT:Int = 6

var i = 0

var mail = Mail(advTemplate: AdvTemplate())
mail.taill = "XX银行版权所有"
while i<MAX_COUNT{
   
    let cloneMail:Mail = mail.copy() as! Mail
    cloneMail.appellation = getRandString(maxLength: 5)+"先生（女士）"
    cloneMail.receiver = getRandString(maxLength: 5)+"@"+getRandString(maxLength: 8)+".com"
    sendMail(mail: cloneMail)
    i+=1
}

/**
 原型模式定义：用原型实例指定创建对象的种类，并且通过拷贝这些原型创建新的对象
 注意：要使用copy方法，类的成员变量上不要增加final关键字
 */

//let randStr = getRandString(maxLength: 5)
//print(randStr)

